function hasVote(str,userid) {
	if(str) {
		let array = str.split(",")
		return array.includes(userid);
	}
	return false;
}
var newVue = new Vue({
	el: '#main',
	data: {
		headerData:{
			'titleColor':'#08acee',
			'title':'我的投票',
			'icon':false,
			'iconColor':'white',
			'titleIcon':'fa-plus-circle'
		},
		replys: [],
		postId: null,
		posterUser: null,
		posterInfo: null,
		posterCounter: null,
		posterContext: null,
		posterContextType: null,
		votes:[],
		preNumber:0,
		submitable: false,
		submitext:'请投票',
		hasVote: false
	},
	mounted: function() {
		this.init();
	},
	methods: {
		init: function() {
			this.postinit();
		},
		voteInit: function() {
			this.preNumber = 0;
			for(var i=0; i<this.posterContext.voteItem.length;i++) {
				this.votes[i] = 0;
			}
		},
		vote: function(item,e) {
			this.voteInit();
			if(!this.hasVote) {
				var el = $(e.target).children("i");
				$(".fa-check").addClass("hidden");
				this.votes[item] = 1;
				this.preNumber = 1;
				this.submitable = true;
				el.removeClass("hidden");
				$("#voteBtn").addClass("activeButton");
			} else {
				alert("投过票了都，别点了^_^");
				return;
			}
		},
		getVote: function(item) {
			return this.votes[item] + this.posterContext.voteItem[item].voteNumber;
		},
		getVotePercent: function(item) {
			if((this.preNumber + this.posterContext.totalNumber) == 0) {
				return "0 %";
			}
			var top = this.votes[item] + this.posterContext.voteItem[item].voteNumber;
			var bottom = this.preNumber + this.posterContext.totalNumber;
			return (Math.ceil((top*100)/bottom))+" %";
		},
		voteSubmit: function() {
			if(this.hasVote) {
				alert("投过票了都，别点了^_^");
				return;
			}
			if(!this.submitable) {
				return;
			}
			var that = this;
			var votesJoin = this.votes.join(",");
			var userId = util.local("userId");
			//增加当前投票用户id
			if(this.posterContext.voteUserIds) {
				if(this.posterContext.voteUserIds.length>0) {
	 				this.posterContext.voteUserIds += ","+userId;
				} else {
					this.posterContext.voteUserIds += userId;
				}
			} else {
				this.posterContext["voteUserIds"] = userId;
			}
			util.tokenJsonPost('https://www.maksdu.com/app/post/vote?postId='+that.postId+'&votes='+votesJoin,JSON.stringify(that.posterContext),function(data) {
				if(data.code == 200) {
					window.location.reload();
				} else {
					window.location.replace("https://www.maksdu.com/login/?redirect="+window.location);
				}
			});
		},
		postinit: function() {
			var that = this;
			var userId = util.local("userId");
			util.get('https://www.maksdu.com/app/post/postlook',{
				postid: that.urlid 
			}, function(data) {
				if(data.code == 200) {
					that.posterUser = data.value.baseMsg.user;
					that.posterInfo = data.value.baseMsg.info;
					that.posterCounter = data.value.baseMsg.counter;
					that.posterContext = data.value.extraMsg.context;
					if(hasVote(that.posterContext.voteUserIds,userId)) {
						that.submitext = '已投票';
						that.hasVote = true; 
					}
					that.posterContextType = data.value.extraMsg.contextType;
					that.posterContextType = data.value.extraMsg.contextType;
					that.postId = data.value.id;
					that.voteInit();
					if(that.posterContextType == 'simple') {
						that.headerData.titleColor = '#44c7fb';
						that.headerData.title = '';
					}
					if(data.value.extraMsg.replys)
						that.replys = data.value.extraMsg.replys;
				}
				
			});
		},
		urlid: function() {
			var url = window.location.search;
			if(url.length>1) {
				url = url.substring(1);
				var params = url.split("=");
				docid = params[1];
				if(docid) {
					return docid;
				} else
					return -1;
			} 
		},
		dateFormatter: function(newDate) {
			var theTime = new Date().getTime();
			var mid = theTime - newDate;

			if(mid < 60 * 1000) {
				return Math.ceil(mid / 1000) +' 秒 前';
			} else if(mid < (3600 * 1000)) {
				return Math.ceil(mid / (60 * 1000))+ '分钟 前';
			} else if(mid < (24*3600 * 1000)) {
				return Math.ceil(mid / (3600 * 1000))+ '小时 前';
			} else {
				return new Date(newDate).Format('MM-dd');
			}
		},
		dateFormat: function(newDate) {
			return new Date(newDate).Format('yyyy-MM-dd HH:mm');
		},
		send: function() {
			var docid = -1;
			var url = window.location.search;
			if(url.length>1) {
				url = url.substring(1);
				var params = url.split("=");
				docid = params[1];
			}
			var that = this;
			util.tokenPost('https://www.maksdu.com/app/post/reply',{
				context: that.content,
				postid: docid,
			}, function(result) {
				
				console.info(result);
				if(!result.login_status) {
					alert("您还没有登录，2s后跳转到登录界面");
					setTimeout(function(){
						window.location.replace("../../login/");
					},2000)
					//window.location.replace("../../login/");
				} else {
					window.location.reload();
				}
			})
		},
		blurFun: function(e, aa) {
            this.content = e.target.textContent
            console.log('innerHTML=', e.target.innerHTML);
            console.log('setText=', aa);
            console.log('e=', e);
            //this.$data[aa] = e.target.innerHTML
        }
	}
});